﻿/********************************************************************************/
// <copyright file="HttpContextLifetimeManager.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="17/12/2012 4:18:36 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Application
{
    using System;
    using System.Web;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// This is a derived type of LifeTimenanager, use it to manage object lifetime within a Http Request Context
    /// http://cnug.co.in/blogs/shijuv/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
    {
        public override object GetValue()
        {
            return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
        }
        public override void RemoveValue()
        {
            HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
        }
        public override void SetValue(object newValue)
        {
            HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
        }
        public void Dispose()
        {
            RemoveValue();
        }
    }
}
